﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using fbasync;
using fbasync.web.Data;

namespace fbasync.web.Controls.TemplateEditor
{
    public partial class ApplicationTemplates : System.Web.UI.UserControl
    {
        public static ApplicationTemplates Instance
        {
            get { return (ApplicationTemplates)HttpContext.Current.Items["ApplicationTemplatesInstance"]; }
            set { HttpContext.Current.Items["ApplicationTemplatesInstance"] = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            Instance = this;

            base.OnInit(e);
        }

        public string ApplicationKey
        {
            get { return (string)ViewState["ApplicationKey"]; }
            set { ViewState["ApplicationKey"] = value; }
        }

        public void BindTemplates()
        {
            DataView dataView = new DataView(TemplateEditorDataAccess.DataSet.StoryTemplateBundle);

            dataView.Sort = "Name asc";

            uiTemplatePager.TotalItems = dataView.Count;

            IList<DataRowView> rowsToShow = new List<DataRowView>();

            int startItem = uiTemplatePager.StartItem;
            int stopItem = startItem + uiTemplatePager.PageSize  - 1;

            for (int rowIndex = 0; rowIndex < dataView.Count; rowIndex++)
            {
                if ((rowIndex >= startItem) && (rowIndex <= stopItem))
                {
                    rowsToShow.Add(dataView[rowIndex]);
                }
            }

            uiApplicationTemplates.DataSource = rowsToShow;
            uiApplicationTemplates.DataBind();

            uiTemplatePager.DataBind();
        }

        public void InitControls()
        {
            if (ApplicationManager.CurrentApplication.FacebookApiKey == ApplicationKey)
            {
                BindTemplates();
            }
            else
            {
                ApplicationKey = ApplicationManager.CurrentApplication.FacebookApiKey;

                Global.Connection.GetRegisteredTemplateBundlesCompleted +=
                    (sender, e) =>
                    {
                        if (null != e.Error) ExceptionHandler.HandleException(e.Error);
                        else
                        {
                            TemplateEditorDataAccess.AssignApplicationTemplates(e.Result);
                        }

                        BindTemplates();
                    };

                Global.Connection.GetRegisteredTemplateBundlesAsync(null);
            }
        }

        protected void ApplicationTemplates_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Control itemControl = e.Item.FindControl("uiTemplateItem");

            if (itemControl is StoryTemplateBundleItem)
            {
                ((StoryTemplateBundleItem)itemControl).AssignTemplateBundle((TemplateEditorDataSet.StoryTemplateBundleRow) ((DataRowView) e.Item.DataItem).Row);
            }
        }

        protected void TemplatePager_ItemClick(object sender, EventArgs e)
        {
            BindTemplates();
        }

        public void ResetPager()
        {
            uiTemplatePager.StartItem = 0;
        }
    }
}